<?php

    // +----------------------------------------------------------------------
    // | 属性模型
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------

    namespace app\event\cms;

    use app\common\event\Base;
    use app\model\cms\Attribute as AttributeModel;
    use app\model\cms\Document as DocumentModel;
    use think\db\Where;

    class Attribute extends Base
    {

        /**
         * 获取字段列表
         * @param int  $pattern_id 当前模型id
         * @param bool $is_edit 新增|编辑模式
         * @return array|\PDOStatement|string|\think\Collection
         * @throws \think\Exception
         * @author 微尘 <yicmf@qq.com>
         * @datetime: 2019/8/10 10:43
         */
        public static function getModelBuilder($pattern_id, $is_edit = true)
        {
            /* 获取属性 */
            $map['status'] = 1;
            $model = DocumentModel::where('id', $pattern_id)->find();
            if ($model) {
                $map['pattern_id'] = ['in', $pattern_id . ',' . $model['extend']];
            } else {
                $map['pattern_id'] = $pattern_id;
            }
            if ($is_edit) {
                $map['is_show'] = ['in', '1,3,5'];
            } else {
                $map['is_show'] = ['in', '1,2,5'];
            }
            return AttributeModel::withAttr('name', function($value, $data) use ($model) {
                if ($model['id'] != $data['pattern_id'] || isset($data['change'])) {
                    return $value;
                } else {
                    return $model['name'] . '.' . $value;
                }
            })->where(new Where($map))->order('id asc')->select();
        }

        /**
         * 根据模型id获取字段属性name集
         * @param integer $pattern_id
         * @return array
         */
        public static function getNamesByModel($pattern_id)
        {
            return AttributeModel::where('pattern_id', $pattern_id)->column('name');
        }

    }
